﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Tilver.Coder.Utility;

namespace Tilver.Coder.Core
{
    [Serializable]
    public class CoderTableKey : CoderObjectBase
    {

        private CoderTable _primaryTable = null;
        private IEnumerable<CoderColumn> _primaryColumns = null;
        private CoderTable _foreignTable = null;
        private IEnumerable<CoderColumn> _foreignColumn = null;

        /// <summary>
        /// 主表完整名称
        /// </summary>
        public string PrimayTableFullName { get; set; }

        /// <summary>
        /// 主表中关联的列名称集合
        /// </summary>
        public List<string> PrimaryColumnNames { get; set; }

        /// <summary>
        /// 外表完整名称
        /// </summary>
        public string ForeignTableFullName { get; set; }

        /// <summary>
        /// 外表中关联的列名称集合
        /// </summary>
        public List<string> ForeignColumnNames { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 主表
        /// </summary>
        [XmlIgnore]
        public CoderTable PrimaryTable
        {
            get
            {
                if (_primaryTable == null)
                    _primaryTable = DataBase.FindTable(PrimayTableFullName);
                return _primaryTable;
            }
        }

        /// <summary>
        /// 主表中关联的列集合
        /// </summary>
        [XmlIgnore]
        public IEnumerable<CoderColumn> PrimaryColumns
        {
            get
            {
                if (_primaryColumns == null)
                    _primaryColumns = PrimaryTable.FindColumns(PrimaryColumnNames);
                return _primaryColumns;
            }
        }

        /// <summary>
        /// 外表
        /// </summary>
        [XmlIgnore]
        public CoderTable ForeignTable
        {
            get
            {
                if (_foreignTable == null)
                    _foreignTable = DataBase.FindTable(ForeignTableFullName);
                return _foreignTable;
            }
        }

        /// <summary>
        /// 外表中关联的列集合
        /// </summary>
        [XmlIgnore]
        public IEnumerable<CoderColumn> ForeignColumns
        {
            get
            {
                if (_foreignColumn == null)
                    _foreignColumn = ForeignTable.FindColumns(ForeignColumnNames);
                return _foreignColumn;
            }
        }
    }
}
